#include <iostream>

//造火箭，造飞机
/**
 *1. 提供一个抽象工厂类
 *2. 提供与产品对应的工厂类
 *3. 提供一个抽象产品类
 *4. 提供一个或多个产品类
 */
class PlaneFactory;
class RocketFactory;

 //抽象产品类
class AbstractProduct {
public:
    virtual void makeProduct() = 0;
};

//抽象工厂类
class AbstractFactory {
public:
    //static AbstractFactory* createFactory(char type) {
    //    switch (type) {
    //    case 'p':
    //        return new PlaneFactory;
    //    case 'r':
    //        return new RocketFactory;
    //    default:
    //        break;
    //    }
    //    return nullptr;
    //}
    virtual AbstractProduct* createProduct() = 0;

};

//产品类
class PlaneProduct : public AbstractProduct {
public:
    void makeProduct() override {
        std::cout << "make a plane" << std::endl;
    }
};

class RocketProduct : public AbstractProduct {
public:
    void makeProduct() override {
        std::cout << "make a rocket" << std::endl;
    }
};

//工厂类
class PlaneFactory : public AbstractFactory {
public:
    AbstractProduct* createProduct() override {
        return new PlaneProduct;
    }
};

class RocketFactory : public AbstractFactory {
public:
    AbstractProduct* createProduct() override {
        return new RocketProduct;
    }
};

int main(int argc, char const* argv[]) {
    AbstractFactory* factory = new PlaneFactory;
    AbstractProduct* product = factory->createProduct();
    product->makeProduct();
    delete product;
    delete factory;
    factory = new RocketFactory;
    product = factory->createProduct();
    product->makeProduct();
    delete product;
    delete factory;
    return 0;
}